<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/16
 * Time: 14:28
 */

namespace app\controller;


use rap\config\Config;
use rap\web\Request;
use app\server\request as reModel;
use app\domain\StickPay;
use app\domain\StickUser;

class PayController
{
    public function getUserUnionOrderNum (Request $request) {

        if ($request->method() == "POST") {

            $phone = $request->param('phone', '');
            $amount = $request->param('amount', '');
            $number = $request->param('number', 0);
            $token = $request->param('token');

            $orderNumber = CommonController::getOrderNumber();
            $random = CommonController::getRandom();
            $config = Config::getFileConfig();

            $con = [
                'trade_no' => $orderNumber,
                'callback' => 'http://mapp.bihuapp.com/Pay/callbackUrl',//http://mapp.bihuapp.com/Pay/callbackUrl
                'amount' => "0.01",
                'title' => '车贴申请',
                'user_id' => $_SESSION[$token]['user_id'],
                'des' => '车贴申请支付',
                'remark' => '车贴申请支付',
                'secret' => $config['app_info']['secret'],
            ];

            $url = $config['backUrl']['base_url'] . $config['backUrl']['order_url'];
            $res = reModel::getMethod($url, $con);

            if ($res['data']['unionOrderNum']){
                $data = [
                    'unionOrderNum' => $res['data']['unionOrderNum'],
                    'des' => '车贴申请支付',
                    'title' => '车贴申请',
                    'secret' => $config['app_info']['secret'],
                    'amount' => "0.01",
                    'trade_no' => $orderNumber,
                ];

                $info = [
                    'user_id' => $_SESSION[$token]['user_id'],
                    'phone' => $phone,
                    'amount' => "0.01",
                    'trade_no' => $orderNumber,
                    'union_order_num' => $res['data']['unionOrderNum'],
                    'pay_type' => 0,
                    'intro' => '',
                    'status' => 0,
                    'create_time' => date('Y-m-d H:i:s', time()),
                ];

                //添加订单信息
                StickPay::addOrderInfo($info);
                //更新申请数量
                StickUser::updatePicCount( $_SESSION[$token]['user_id'],$number);

                //返回结果
                return CommonController::success("获取成功", $data);
            } else {
                return CommonController::false(404, '获取订单信息失败', []);
            }
        }
    }

    public function callbackUrl (Request $request){
        if ($request->method() == "GET") {
            return;
        }
    }

    public function updateOrderStatus(Request $request){
        if ($request->method() == "POST") {
            $token = $request->param('token', '');
            $union_order_num = $request->param('union_order_num', '');
            $type = $request->param('union_order_num', 1);
            $status = $request->param('status', 1);

            $where = [
                'user_id' => $_SESSION[$token]['user_id'],
//                'user_id' => 3225,
                'union_order_num' => $union_order_num,
            ];


            $data = [
//                'pay_type' => $type,
                'status' => $status
            ];

            $res = StickPay::updateOrderInfo($where, $data);

            if ($res) {
                return CommonController::success("支付成功", $data);
            } else {
                return CommonController::false(404, '支付失败', []);
            }

        }
    }


}